<?php

namespace app\admin\controller\store;

use app\admin\model\store\GoodsSpecifica;
use app\common\controller\AdminController;
use app\common\service\store\SpeciService;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\Exception;
use think\facade\View;

/**
 * @ControllerAnnotation(title="goods_specifica")
 */
class Specifica extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\store\GoodsSpecifica();

    }

    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $data = SpeciService::speciList($page, $limit, $where);
            return json($data);
        }
        return $this->fetch();
    }

    public function add($id = 0)
    {
        View::assign('id', $id);
        return View::fetch();
    }

    public function create()
    {
        $post = $this->request->post();
        $rule = [
            'specifica_name|规格名' => 'require|unique:goods_specifica,specifica_name',
        ];
        $this->validate($post, $rule);
        $save = SpeciService::speciSave($post);
        if ($save) {
            return json(['status' => 200, 'msg' => '添加成功']);
        } else {
            return json(['status' => 500, 'msg' => '添加失败']);
        }
    }

    public function edit($id = 0)
    {
        $row = $this->model->find($id);
        View::assign('row', $row);
        View::assign('id', $id);
        return View::fetch();
    }

    public function update()
    {
        $post = $this->request->post();
        $rule = [];
        $this->validate($post, $rule);
        $save = SpeciService::speciEdit($post);
        $save['code']==0 ? $this->success('修改成功') : $this->error('修改失败');
    }


}






